package com.zwcloud.zw.railway.utils;

import com.zwcloud.zw.railway.entity.Destination;
import com.zwcloud.zw.railway.service.impl.DestinationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class DestinationInitializer {

    @Autowired
    private DestinationService destinationService;

    private static List<Destination> destinationList;

    @EventListener(ContextRefreshedEvent.class)
    public void init() {
        try {
            destinationList = destinationService.getDestinationList();
            System.out.println("目的地列表初始化成功，数量: " + (destinationList != null ? destinationList.size() : 0));
        } catch (Exception e) {
            System.err.println("目的地列表初始化失败: " + e.getMessage());
            throw new RuntimeException("Failed to initialize destination list", e);
        }
    }

    public static List<Destination> getDestinationList() {
        return destinationList;
    }
}